package com.woniuxy.third.controller;

import com.woniuxy.third.annotation.RequirePermission;
import com.woniuxy.third.entity.Student;
import com.woniuxy.third.entity.User;
import com.woniuxy.third.result.ResponseResult;
import com.woniuxy.third.service.StudentService;
import com.woniuxy.third.service.UserService;
import com.woniuxy.third.utils.FileUploadUtil;
import com.woniuxy.third.utils.GetUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/student")
@Api(value = "学生模块", tags = "StudentApi")
public class StudentController {
    @Resource
    private StudentService studentService;
    @Resource
    private UserService userService;

    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    //指定访问该方法时的权限
    @RequirePermission("student:find")
    @GetMapping("/all")
    @ApiOperation(value = "查询学生",notes = "返回学生的信息")
    public ResponseResult<Student> student(HttpServletRequest request) {

        //根据账户查询到user(调用工具类)
        User user = GetUserUtil.getUser(request);

        //根据id查找学生
        Student student = studentService.findStudentById(user.getId());

        //将数据进行封装返回
        return new ResponseResult<Student>(200,"success",student);
    }

    @RequirePermission("student:add")
    @PostMapping("/add")
    @ApiOperation(value ="添加学生", notes = "返回boolean值，true表示成功，false表示失败")
    public ResponseResult<String> addStudent(HttpServletRequest request) {

        //根据账户查询到user(调用工具类)
        User user = GetUserUtil.getUser(request);
        //根据id查找学生
        Student studentById = studentService.findStudentById(user.getId());
        //如果未查询到学生数据，则创建一条学生信息
        if(studentById == null){
            //创建学生对象
            Student student = new Student();
            //设置学生id（与user的id一致）
            student.setId(user.getId());
            //添加信息
            boolean addStudent = studentService.addStudent(student);
            //返回结果
            if(addStudent){
                logger.debug("学生个人信息创建成功");
                return new ResponseResult<>(200,"success","学生个人信息创建成功");

            }else{
                logger.debug("学生个人信息创建失败");
                return new ResponseResult<>(500,"fail","学生个人信息创建失败");
            }
        }else{
            return new ResponseResult<>(201,"success","学生个人信息已存在");
        }
    }

    @RequirePermission("student:update")
    @PutMapping("/update")
    @ApiOperation(value ="修改学生信息", notes = "返回boolean值，true表示成功，false表示失败")
    public ResponseResult<String> update(@RequestBody Student student,HttpServletRequest request) {

        //根据账户查询到user(调用工具类)
        User user = GetUserUtil.getUser(request);

        //设置学生id（与user的id一致）
        student.setId(user.getId());

        boolean updateStudent = studentService.updateStudent(student);

        //返回结果
        if(updateStudent){
            logger.debug("学生个人信息修改成功");
            return new ResponseResult<>(200,"success","学生个人信息修改成功");

        }else{
            logger.debug("学生个人信息修改失败");
            return new ResponseResult<>(500,"fail","学生个人信息修改失败");
        }

    }

    @PostMapping("/updateImage")
    @ApiOperation(value = "添加头像", notes = "请选择上传照片,格式为jpg或者png返回对象信息")
    public ResponseResult<String> updateImage(HttpServletRequest request) {
        System.out.println(111);
        List<String> list = FileUploadUtil.multifileUpload(request);
        System.out.println(list);
        if (list.size() == 2) {
            if (list.get(1).equals("保存成功")) {
                String image = list.get(0);
                int id = GetUserUtil.getUser(request).getId();
                studentService.imageUpload(image, id);
                return new ResponseResult<>(200, list.get(1), null);
            }
            return new ResponseResult<>(500, list.get(1), null);
        }
        return new ResponseResult<>(500, "请上传一个文件", null);
    }
}
